package com.imti.retrofitweatherdemo.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener;
import com.google.android.material.tabs.TabLayout.Tab;
import com.imti.retrofitweatherdemo.BaseApplication;
import com.imti.retrofitweatherdemo.R;
import com.imti.retrofitweatherdemo.logic.HomeDocData;
import com.imti.retrofitweatherdemo.logic.INewsLogic;
import com.imti.retrofitweatherdemo.logic.NewsLogic;
import com.imti.retrofitweatherdemo.logic.NewsModelListItem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeFragment extends Fragment implements INewsLogic
{

    private View mRootView;

    private TabLayout mHomeTab;

    private ViewPager mHomePager;

    private List<NewsListFragment> mNewsFragmentList = new ArrayList<>();

    private HomePagerAdapter mHomePagerAdapter;

    private NewsLogic mNewsLogic;

    private ArrayList<String> mTabList = new ArrayList<String>(
        Arrays.asList("头条", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "时尚", "游戏", "汽车", "健康"));

    private int mCurTabPosition = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mNewsLogic = NewsLogic.getInstance((BaseApplication) getActivity().getApplication(),
            getActivity());
        mNewsLogic.setiNewsLogic(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(R.layout.fragment_home, container, false);

        initView();

        return mRootView;
    }

    private void initView()
    {
        mHomeTab = mRootView.findViewById(R.id.news_home_tab);
        mHomePager = mRootView.findViewById(R.id.news_home_pager);

        mHomePagerAdapter = new HomePagerAdapter(getActivity().getSupportFragmentManager(),
            FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

        if (!mNewsFragmentList.isEmpty())
        {
            mNewsFragmentList.clear();
        }

        for (int i = 0; i < mTabList.size(); i++)
        {
            mNewsFragmentList.add(new NewsListFragment());
        }

        mHomePager.setAdapter(mHomePagerAdapter);
        mHomeTab.setupWithViewPager(mHomePager);

        mHomePager.addOnPageChangeListener(new OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }
            @Override
            public void onPageSelected(int position)
            {

                mCurTabPosition = position;
                mNewsLogic.requestNewsByType(mTabList.get(mCurTabPosition));
            }
            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });

        mNewsLogic.requestNewsByType(mTabList.get(mCurTabPosition));

        mNewsLogic.requestDocList();
    }

    @Override
    public void requestNewsList(List<NewsModelListItem> resultList)
    {
        NewsDataModel.getInstance().setNewModelList(resultList);
    }

    @Override
    public void requestDocList(List<HomeDocModel> docList)
    {

    }

    class HomePagerAdapter extends FragmentPagerAdapter
    {
        public HomePagerAdapter(@NonNull FragmentManager fm, int behavior)
        {
            super(fm, behavior);
        }

        //这是装了一堆Fragment对象的的List<Fragment>
        //for (int i = 0; i < mTabList.size(); i++)
        //        {
        //            mNewsFragmentList.add(new NewsListFragment());
        //        }
        @NonNull
        @Override
        public Fragment getItem(int position)
        {
            return mNewsFragmentList.get(position);
        }

        @Override
        public int getCount()
        {
            return mNewsFragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position)
        {
            return mTabList.get(position);
        }
    }
}